# coding:utf-8
from typing import Dict, List
from views.template_page_view import BasePageTemplateView, TableDataDto
from .common.translator import Translator
from common.utils.utils_json_replace import JsonPlaceholderReplacer
from common.utils.utils_log import AntLogger
from views.common.signal_bus import signalBus


class DemoCommonView(BasePageTemplateView):
    """Scroll interface"""

    def __init__(self, parent=None):
        t = Translator()
        super().__init__("DemoCommonView", parent=parent)

        # 添加一个 Table
        self.addTableTemplate("当前股票", self.getStockTableInfo, self.getBaseParam, 1)

        # 添加一个 EChat
        self.addEchatTemplate(
            "大盘风险", "demo_chat", self.exchangeMainStockRisk, self.getBaseParam, 1
        )

    def getBaseParam(self) -> Dict[str, str]:
        return {"stockCode": "100001"}

    def exchangeMainStockRisk(self, jsonData: str, params: Dict[str, str]) -> str:
        """大盘 Echat 分线报表数据"""
        source_data = [
            {"value": 40, "name": "连续涨停"},
            {"value": 38, "name": "股价过高"},
            {"value": 32, "name": "呈连续下跌趋势"},
            {"value": 30, "name": "成家额下降大量卖出"},
            {"value": 28, "name": "节前风险时段"},
            {"value": 26, "name": "已盈利20以上"},
            {"value": 22, "name": "财报周期"},
            {"value": 18, "name": "股价新高"},
        ]

        replacer = JsonPlaceholderReplacer(jsonData)
        replacer.replace_placeholder("__NODE_POINT__", source_data)
        replacer.replace_placeholder("__CHAT_NAME__", "风险指标")
        return replacer.get_modified_json()

    def getStockTableInfo(self, params: Dict[str, str]) -> str:
        AntLogger.info(f"getStockTableInfo 传入查询的对象信息{params}")
        return TableDataDto(
            table_header=[
                "股票代码",
                "股票名称",
                "股票当前价",
                "当前盈亏比例",
                "当前风险等级",
            ],
            table_data=[
                ["600519", "贵州茅台", "1800.00", "+5.32%", "低风险"],
                ["000858", "五粮液", "120.00", "-2.15%", "中风险"],
                ["002304", "洋河股份", "95.50", "+1.25%", "高风险"],
            ],
            table_item_fun=[{"查看详情": self.showTableDatail}],
        )

    def doSearch(self):
        AntLogger.info(f"查询股票{self.searchKey}")

    def showTableDatail(self, table_data: List[str]) -> str:
        # 此处进行页面跳转
        signalBus.switchToSampleCard.emit("demoChatView", 0, table_data[0])
